﻿using System;
using System.Collections.Generic;
using System.Text;
using LibGUI.Chat;
using LibGUI.LogOn;
using LibGUI.MainForm;
using LibGUI.Schedule;
using LibRTC.Core;
using LibRTC.Core.MessageMap;
using LibRTC.Core.MessageMap.BuddyEvent;
using LibRTC.Core.MessageMap.ClientEvent;
using LibRTC.Core.MessageMap.IntensityEvent;
using LibRTC.Core.MessageMap.MediaEvent;
using LibRTC.Core.MessageMap.MessagingEvent;
using LibRTC.Core.MessageMap.ParticipantStateChangeEvent;
using LibRTC.Core.MessageMap.PresenceStatusEvent;
using LibRTC.Core.MessageMap.ProfileEvent;
using LibRTC.Core.MessageMap.RegistrationStateChangeEvent;
using LibRTC.Core.MessageMap.SessionStateChangeEvent;
using LibRTC.Core.MessageMap.WatcherEvent;
using RTCCore;

namespace LibAssemble.Assemble
{
    /// <summary>
    /// This class is ued to Assemble all the component
    /// Set interface
    /// </summary>
    public class Assemble
    {
        /// <summary>
        /// LogOn Form, user need to input username/password to logon
        /// </summary>
        public LogOnForm logOnForm;

        public PresenceUI presenceUI;

        /// <summary>
        /// rtc core client, deal with all RTC operations
        /// </summary>
        public RTCCoreClient rtcCoreClient;

        /// <summary>
        /// MainForm
        /// </summary>
        public MainForm mainform;

        /// <summary>
        /// Dispatch message to different chat Form
        /// </summary>
        public MessageDispatcher messageDispatcher;

        /// <summary>
        /// Do Assemble, config all the objects
        /// </summary>
        /// <returns>indicate whether assemble is successful</returns>
        public bool DoAssemble()
        {
            // first, create all the object
            logOnForm = new LogOnForm();
            presenceUI = new PresenceUI();
            rtcCoreClient = new RTCCoreClient();
            mainform = new MainForm();
            messageDispatcher = new MessageDispatcher();

            IntensityHandler intesityHandler = new IntensityHandler();
            ParticipantStateChangeHandler participantStateChangeHandler = new ParticipantStateChangeHandler();
            AuthHandler authHandler = new AuthHandler();
            LogInHandler logInHandler = new LogInHandler();
            CleanupHandler cleanupHandler = new CleanupHandler();
            ProfileGetHandler profileGetHandler = new ProfileGetHandler();
            SessionIMHandler sessionImHandler = new SessionIMHandler();
            SessionPCToPCHandler sessionPctoPcHandler = new SessionPCToPCHandler();
            VideoHandler videoHandler = new VideoHandler();
            MessagingHandler messagingHandler = new MessagingHandler();
            WatcherHandler watcherHandler = new WatcherHandler();
            BuddyStatusHandler buddyStatusHandler = new BuddyStatusHandler();
            PresenceStatusHandler presenceStatusHandler = new PresenceStatusHandler();


            //config all the component through setter functions
            logOnForm.SetRTCOperations(rtcCoreClient);

            rtcCoreClient.SetIPresenceUI(presenceUI);
            rtcCoreClient.SetLogOnUI(logOnForm);

            messageDispatcher.SetParentForm(mainform);
            messageDispatcher.SetRTCCoreOperations(rtcCoreClient);

            // set variables
            participantStateChangeHandler.SetRTCCoreClient(rtcCoreClient);
            authHandler.SetRTCCoreClient(rtcCoreClient);
            logInHandler.SetRTCCoreClient(rtcCoreClient);
            cleanupHandler.SetRTCCoreClient(rtcCoreClient);
            profileGetHandler.SetRTCCoreClient(rtcCoreClient);
            sessionImHandler.SetRTCCoreClient(rtcCoreClient);
            sessionPctoPcHandler.SetRTCCoreClient(rtcCoreClient);
            videoHandler.SetRTCCoreClient(rtcCoreClient);
            buddyStatusHandler.SetBuddyStatusChange(mainform);

            // register all message handlers
            RTCMessageMap.GetInstance().RegisterEventHandler(RTC_EVENT.RTCE_INTENSITY,intesityHandler);
            RTCMessageMap.GetInstance().RegisterEventHandler(RTC_EVENT.RTCE_PARTICIPANT_STATE_CHANGE, participantStateChangeHandler);
            RTCMessageMap.GetInstance().RegisterEventHandler(RTC_EVENT.RTCE_REGISTRATION_STATE_CHANGE, authHandler);
            RTCMessageMap.GetInstance().RegisterEventHandler(RTC_EVENT.RTCE_REGISTRATION_STATE_CHANGE, logInHandler);
            RTCMessageMap.GetInstance().RegisterEventHandler(RTC_EVENT.RTCE_CLIENT, cleanupHandler);
            RTCMessageMap.GetInstance().RegisterEventHandler(RTC_EVENT.RTCE_PROFILE, profileGetHandler);
            RTCMessageMap.GetInstance().RegisterEventHandler(RTC_EVENT.RTCE_SESSION_STATE_CHANGE, sessionImHandler);
            RTCMessageMap.GetInstance().RegisterEventHandler(RTC_EVENT.RTCE_SESSION_STATE_CHANGE, sessionPctoPcHandler);
            RTCMessageMap.GetInstance().RegisterEventHandler(RTC_EVENT.RTCE_MEDIA, videoHandler);
            RTCMessageMap.GetInstance().RegisterEventHandler(RTC_EVENT.RTCE_MESSAGING, messagingHandler);
            RTCMessageMap.GetInstance().RegisterEventHandler(RTC_EVENT.RTCE_WATCHER, watcherHandler);
            RTCMessageMap.GetInstance().RegisterEventHandler(RTC_EVENT.RTCE_BUDDY, buddyStatusHandler);
            RTCMessageMap.GetInstance().RegisterEventHandler(RTC_EVENT.RTCE_PRESENCE_STATUS, presenceStatusHandler);

            BaseChatForm.SetMessagingRegister(messagingHandler);
            BaseChatForm.SetIMSessionChangeRegister(sessionImHandler);
            BaseChatForm.SetVideoSessionChangeRegister(sessionPctoPcHandler);

            sessionImHandler.SetISessionNew(messageDispatcher);
            sessionPctoPcHandler.SetISessionNew(messageDispatcher);
            videoHandler.SetIVideoEvent(messageDispatcher);

            return true;
        }

        /// <summary>
        /// Do some clean work
        /// </summary>
        public void DoClean()
        {
            rtcCoreClient.DoShutdown();
        }
    }
}